package com.example.checkqufuface.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.R;
import com.example.checkqufuface.bean.QufuQueryStaffReceiveTicketResultBean;

import java.util.List;

public class SearchFaceAdapter extends RecyclerView.Adapter<SearchFaceAdapter.ViewHolder> {

    private Context context;
    private List<QufuQueryStaffReceiveTicketResultBean.DataBean> dataBeanList;
    private static SetfaceAdd setfaceAdd;

    public SearchFaceAdapter(Context context, List<QufuQueryStaffReceiveTicketResultBean.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_face_search_show, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

        holder.tvTicketName.setText(dataBeanList.get(position).getScenicTicketName());
        holder.tvPeopleName.setText(dataBeanList.get(position).getCertificateName());
        holder.tvIdType.setText(dataBeanList.get(position).getCertificateTypeName());
        holder.tvIdNumber.setText(hideId(dataBeanList.get(position).getCertificateNo()));
        if (StringUtil.isNullOrEmpty(dataBeanList.get(position).getFacePicPath())) {
            holder.tvFacePick.setText("0/1");
        }else {
            holder.tvFacePick.setText("1/1");
        }

        holder.btFaceAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setfaceAdd.faceAdd(dataBeanList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTicketName;
        private final TextView tvPeopleName;
        private final TextView tvIdType;
        private final TextView tvIdNumber;
        private final TextView tvFacePick;
        private final Button btFaceAdd;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTicketName = itemView.findViewById(R.id.tv_face_show_tickte_name);
            tvPeopleName = itemView.findViewById(R.id.tv_face_show_people_name);
            tvIdType = itemView.findViewById(R.id.tv_face_show_id_type);
            tvIdNumber = itemView.findViewById(R.id.tv_face_show_id_number);
            tvFacePick = itemView.findViewById(R.id.tv_face_show_face_pick);
            btFaceAdd = itemView.findViewById(R.id.bt_face_show_add);
        }
    }

    public static String hideId(String id){
        if (!StringUtil.isNullOrEmpty(id)){
            StringBuilder stringBuilder = new StringBuilder(id);
            stringBuilder.replace(4, 14, "********");
            return stringBuilder.toString();
        }else {
            return "";
        }
    }

    public interface SetfaceAdd{

        void faceAdd(QufuQueryStaffReceiveTicketResultBean.DataBean dataBean);
    }

    public  void setfaceAddListener(SetfaceAdd setface){
        setfaceAdd = setface;
    }
}
